<?php

use console\db\Migration;
use yii\db\Query;

/**
 * 乐视视频时长更新
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300010_000005_t_cms_article_video_fill_duration extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%cms_article_video}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $query = (new Query())->from('{{%video_lecloud}}');
        $this->handleProgress($query, [$this, 'evenDule'], null, Yii::$app->olddb);
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        $this->update($this->tableName, [
            'duration' => 0,
            'size' => 0,
        ], [
            '!=', 'le_id', 0
        ]);
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     */
    public static function evenDule($migration, $rows) {
        foreach ($rows as $row) {
            $migration->db->createCommand()->update($migration->tableName, [
                'duration' => $row['video_duration'],
                'size' => $row['initial_size'],
            ], [
                'le_id' => $row['video_id'],
                'le_unique' => $row['video_unique'],
            ])->execute();
        }
    }

}
